package com.yc.cloud.admin.controller;

import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.orm.CommonPage;
import com.yc.cloud.common.basic.dto.BasePageReqDTO;
import com.yc.cloud.admin.dto.request.HwCameraStorageStrategyCreateReqDTO;
import com.yc.cloud.admin.dto.request.HwCameraStorageStrategyUpdateReqDTO;
import com.yc.cloud.admin.dto.request.StrategyTimeslotBindReqDTO;
import com.yc.cloud.admin.dto.request.StrategyTimeslotUnbindReqDTO;
import com.yc.cloud.admin.vo.HwCameraStorageStrategyVo;
import com.yc.cloud.admin.vo.HwCameraTimeSlotVo;
import com.yc.cloud.admin.service.HwCameraStorageStrategyService;
import com.yc.cloud.admin.service.HwCameraTimeSlotService;

import java.util.List;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;

/**
 * <p>
 * 摄像头存储策略主表 前端控制器
 * </p>
 *
 * @author kinggu
 * @since 2025-09-15
 */
@RestController
@Api(tags = "摄像头存储策略主表")
@RequestMapping("/hw-camera-storage-strategies")
public class HwCameraStorageStrategyController {

    @Resource
    private HwCameraStorageStrategyService hwCameraStorageStrategyService;
    
    @Resource
    private HwCameraTimeSlotService hwCameraTimeSlotService;

    @ApiOperation("创建摄像头存储策略主表")
    @PostMapping
    public ResponseResult<HwCameraStorageStrategyVo> create(@Validated @RequestBody HwCameraStorageStrategyCreateReqDTO createReqDTO) {
        HwCameraStorageStrategyVo newVo = hwCameraStorageStrategyService.create(createReqDTO);
        if (newVo != null) {
            return ResponseResult.success(newVo);
        }
        return ResponseResult.failed("创建失败");
    }

    @ApiOperation("更新摄像头存储策略主表")
    @PutMapping("/{id}")
    public ResponseResult<HwCameraStorageStrategyVo> update(@PathVariable("id") Long id, @Validated @RequestBody HwCameraStorageStrategyUpdateReqDTO updateReqDTO) {
        updateReqDTO.setId(id);
        HwCameraStorageStrategyVo updatedVo = hwCameraStorageStrategyService.update(updateReqDTO);
        if (updatedVo != null) {
            return ResponseResult.success(updatedVo);
        }
        return ResponseResult.failed("更新失败");
    }

    @ApiOperation("删除摄像头存储策略主表")
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@PathVariable("id") Long id) {
        boolean success = hwCameraStorageStrategyService.delete(id);
        if (success) {
            return ResponseResult.success(true);
        }
        return ResponseResult.failed("删除失败");
    }

    @ApiOperation("获取摄像头存储策略主表详情")
    @GetMapping("/{id}")
    public ResponseResult<HwCameraStorageStrategyVo> getById(@PathVariable("id") Long id) {
        return ResponseResult.success(hwCameraStorageStrategyService.getVOById(id));
    }

    @ApiOperation("分页查询摄像头存储策略主表")
    @GetMapping
    public ResponseResult<CommonPage<HwCameraStorageStrategyVo>> list(@Validated BasePageReqDTO pageReqDTO) {
        return ResponseResult.success(hwCameraStorageStrategyService.list(pageReqDTO));
    }

    @ApiOperation("策略绑定时间段")
    @PostMapping("/{id}/bind-timeslots")
    public ResponseResult<Boolean> bindTimeslots(@PathVariable("id") Long id, @Validated @RequestBody StrategyTimeslotBindReqDTO bindReqDTO) {
        bindReqDTO.setStrategyId(id);
        boolean success = hwCameraStorageStrategyService.bindTimeslots(bindReqDTO);
        if (success) {
            return ResponseResult.success(true);
        }
        return ResponseResult.failed("绑定失败");
    }

    @ApiOperation("解绑策略时间段")
    @PostMapping("/{id}/unbind-timeslots")
    public ResponseResult<Boolean> unbindTimeslots(@PathVariable("id") Long id, @Validated @RequestBody StrategyTimeslotUnbindReqDTO unbindReqDTO) {
        unbindReqDTO.setStrategyId(id);
        boolean success = hwCameraStorageStrategyService.unbindTimeslots(unbindReqDTO);
        if (success) {
            return ResponseResult.success(true);
        }
        return ResponseResult.failed("解绑失败");
    }

    @ApiOperation("获取策略绑定的时间段列表")
    @GetMapping("/{id}/timeslots")
    public ResponseResult<List<HwCameraTimeSlotVo>> getStrategyTimeslots(@PathVariable("id") Long strategyId) {
        return ResponseResult.success(hwCameraTimeSlotService.getTimeslotsByStorageStrategyId(strategyId));
    }
}
 